package com.mz.mattress.client.timer.impl;

import com.mz.mattress.client.mapper.MemberCardMapper;
import com.mz.mattress.client.timer.TimerTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 卡项 每天定时任务，更新过期卡项
 */
@Slf4j
@Component
public class CardExpireTask implements TimerTask {

    @Autowired
    private MemberCardMapper memberCardMapper;

    private LocalDate lastDate;

    @Override
    public boolean canDo() {

        if (lastDate == null) {
            lastDate = LocalDate.now();
            log.info("can_updateCardExpire");
            return true;
        }

        LocalDate now = LocalDate.now();
        if (lastDate.isBefore(now)) {
            lastDate = now;
            log.info("can_updateCardExpire");
            return true;
        }

        return false;
    }

    @Override
    public void run() {

        log.info("run_updateCardExpire");

        memberCardMapper.updateCardExpire();

    }

}
